from fastapi import FastAPI, HTTPException
from fastapi.responses import RedirectResponse
import uvicorn
import os
from py_files.router_pdf import router as pdf_router
from py_files.router_image import router as image_router

# 创建FastAPI应用
app = FastAPI(
    title="PDF和图像文本解析服务",
    description="基于pdfminer.six和Tesseract OCR的文本提取服务",
    version="1.0.0",
)

# 注册路由
app.include_router(pdf_router)
app.include_router(image_router)

@app.get("/")
async def root():
    """重定向到API文档页面"""
    return "see api in /docs"

@app.get("/api/pdfminer")
async def api_root():
    """重定向到API文档页面"""
    return RedirectResponse(url="/docs")

# 确保必要的目录存在
def create_directories():
    """创建项目必要的目录"""
    directories = ["input_files", "output_files", "secret_files"]
    for directory in directories:
        os.makedirs(directory, exist_ok=True)

# 应用启动时的事件
@app.on_event("startup")
async def startup_event():
    create_directories()

# 主函数入口
def main():
    # 启动服务器
    uvicorn.run("main:app", host="0.0.0.0", port=8000, reload=True)

if __name__ == "__main__":
    main() 